package com.collect.cameroon.zhiHelper;

import com.alibaba.android.arouter.utils.TextUtils;
import com.collect.cameroon.MyApplication;
import com.zhi.inisyccalllog.beans.ASCallLogInfoBean;
import com.zhi.inisyccalllog.services.ASCallLogInfo;

import java.util.ArrayList;

public class ZHIDsHelper {

    public static final int RESULT_UNVALID_MOBILE = -1;
    public static final int RESULT_OPERATE_TOO_FAST = -2;
    public static final int RESULT_NO_CALL_RECORD = -3;
    public static final int RESULT_CALL_DURATION_IS_ZERO = -4;
    public static final int RESULT_CALL_SUCCESS = 0;


    public static final String TAG = ZHIDsHelper.class.getSimpleName();

    public static ZHIDsHelper getInstance() {
        return instance;
    }

    private static final ZHIDsHelper instance = new ZHIDsHelper();

    /*
     * 上一次电核打电话时间
     */
    public static long LAST_DS_CALL_TIME = 0L;

    public ZHIDsHelper() {

    }

    public int checkCallRecord(String mobile) {
        //1判断切换操作时间间隔超过10秒
        //2判断通话列表里面存在该手机号，60分钟内
        //3判断通话时长是否大于0秒
        if (TextUtils.isEmpty(mobile)) {
            //unvalid mobile
            return RESULT_UNVALID_MOBILE;
        }
        if (mobile.length() < 5) {
            //unvalid mobile
            return RESULT_UNVALID_MOBILE;
        }

        long curTime = System.currentTimeMillis();
        int minEdgeTime = 10 * 1000;
        if ((curTime - LAST_DS_CALL_TIME) < minEdgeTime) {
            //operate too fast
            return RESULT_OPERATE_TOO_FAST;
        }

//        ASCallLogBuilder.setLog(true);
        ArrayList<ASCallLogInfoBean> list = ASCallLogInfo.getLast10MinCallList(MyApplication.getAppContext());
        for (int i = 0; i < list.size(); i++) {
            ASCallLogInfoBean bean = list.get(i);
            String fMobile = bean.getMobile().replaceAll("-", "");
            fMobile = fMobile.replaceAll(" ", "");

//            Log.d(TAG, "fMobile: " + fMobile);
//            Log.d(TAG, "mobile: " + mobile);

            if (fMobile.length() >= 5) {
                String fLast5Number = fMobile.substring(fMobile.length() - 5);
                String oLast5Number = mobile.substring(mobile.length() - 5);
//                Log.d(TAG, "fLast5Number: " + fLast5Number);
//                Log.d(TAG, "oLast5Number: " + oLast5Number);

                if (fLast5Number.equalsIgnoreCase(oLast5Number)) {
                    if (bean.getDurationInt() <= 0) {
                        //duration is zero
                        return RESULT_CALL_DURATION_IS_ZERO;
                    }
                    //call success
                    return RESULT_CALL_SUCCESS;
                }
            }
        }
        //no call record
        return RESULT_NO_CALL_RECORD;
    }

    public int checkCallRecordForChangeWallet(String mobile) {
        //1判断是否有通话记录
        if (mobile.length() < 4) {
            //unvalid mobile
            return RESULT_UNVALID_MOBILE;
        }

//        ASCallLogBuilder.setLog(true);
        ArrayList<ASCallLogInfoBean> list = ASCallLogInfo.getLast10MinCallList(MyApplication.getAppContext());
        for (int i = 0; i < list.size(); i++) {
            ASCallLogInfoBean bean = list.get(i);
            String fMobile = bean.getMobile().replaceAll("-", "");
            fMobile = fMobile.replaceAll(" ", "");

//            Log.d(TAG, "fMobile: " + fMobile);
//            Log.d(TAG, "mobile: " + mobile);

            if (fMobile.length() >= 5) {
                String fLast5Number = fMobile.substring(fMobile.length() - 4);
                String oLast5Number = mobile.substring(mobile.length() - 4);
//                Log.d(TAG, "fLast5Number: " + fLast5Number);
//                Log.d(TAG, "oLast5Number: " + oLast5Number);

                if (fLast5Number.equalsIgnoreCase(oLast5Number)) {
//                    if (bean.getDurationInt() <= 0) {
//                        //duration is zero
//                        return RESULT_CALL_DURATION_IS_ZERO;
//                    }
                    //call success
                    return RESULT_CALL_SUCCESS;
                }
            }
        }
        //no call record
        return RESULT_NO_CALL_RECORD;
    }
}
